package uk.co.agileworks.bidpirate.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import uk.co.agileworks.bidpirate.service.Pager;

@Controller
public class ListBidsController extends BaseController {
	
	public static final String REDIRECT = "redirect:/listBids.html";

	// TODO check for url hacking
	// TODO error when clicked twice
	@RequestMapping("/listBids-deleteBid.html")
	public String deleteBid(@RequestParam("id") long id) {
		businessFacade.deleteBid(id);
		return REDIRECT;
	}

	// TODO item ended early - also need to update date - bf.updateItem?
	@RequestMapping("/listBids-updatePrice.html")
	public String updatePrice(@RequestParam("itemNumber") long itemNumber) {
		double price = ebayFacade.getItem(itemNumber).getPrice();
		businessFacade.updatePrice(itemNumber, price);
		return REDIRECT;
	}
	
	@RequestMapping("/listBids.html")
	public void listBids(Model model,
			@RequestParam(value = "page", required = false) Integer page) {
		Pager pager = new Pager(businessFacade.listBids(userSession.getUser()));
		pager.setCurrentPage(userSession.getCurrentPage(page));
		model.addAttribute(pager);
		model.addAttribute("alerts", businessFacade.getAlerts(pager.getPageResults()));
		model.addAttribute("openItemUrl", getOpenItemUrl());
	}

}
